package com.niit.controller;


import com.niit.entity.Student;
import com.niit.mapper.StudentMapper;
import com.niit.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

/**
 * @author hanli
 * @date 2025/3/4
 * @description TODO
 */
public class StudentController {
    public static void main(String[] args) {
        // 2020204646
        // 1. 建立连接
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        // 2. getMapper 生成一个代理类 临时的class implememts StudentMapper
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = studentMapper.getStudentBySid("2020204646");
        System.out.println(student.getSname());
        // 关闭连接
        sqlSession.close();

        // 1. 建立连接
//        SqlSession sqlSession2 = MybatisUtil.getSqlSession();
//        // 2. getMapper 生成一个代理类 临时的class implememts StudentMapper
//        StudentMapper studentMapper2 = sqlSession2.getMapper(StudentMapper.class);
//        Student student2 = studentMapper2.getStudentBySid("2020204646");
//        System.out.println(student2.getSname());
//        // 关闭连接
//        sqlSession.close();
        // -----------------------添加学生信息---------------------------------------
//        SqlSession insertSession = MybatisUtil.getSqlSession();
//        StudentMapper insertStudentMapper = insertSession.getMapper(StudentMapper.class);
//
//        Student insertStudent = new Student("1002", "hanliang", "654321", "男", "1001");
//        int insertResult= insertStudentMapper.insertStudent(insertStudent);
//        // 提交事务
//        insertSession.commit();
//
//        if(insertResult>0){
//            System.out.println("添加成功");
//        }else{
//            System.out.println("添加失败");
//        }
//        insertSession.close();

        // -----------------------更新学生信息---------------------------------------
//        SqlSession updateSession = MybatisUtil.getSqlSession();
//        StudentMapper updateStudentMapper = updateSession.getMapper(StudentMapper.class);
//
//        Student updateStudent = new Student("1002", "hanliang_update", "123456", "男", "1001");
//        int updateResult = updateStudentMapper.updateStudent(updateStudent);
//        // 提交事务
//        updateSession.commit();
//
//        if(updateResult > 0){
//            System.out.println("更新成功");
//        }else{
//            System.out.println("更新失败");
//        }
//        updateSession.close();
//
//        // -----------------------删除学生信息---------------------------------------
//        SqlSession deleteSession = MybatisUtil.getSqlSession();
//        StudentMapper deleteStudentMapper = deleteSession.getMapper(StudentMapper.class);

//        // 调用 session 的 selectOne, selectList, insert,update,delete 等方法，指定SQL的ID方式 执行特定SQL
//        int deleteResult = deleteSession.delete("com.niit.mapper.StudentMapper.deleteStudentBySid", "1002");
////        int deleteResult = deleteStudentMapper.deleteStudentBySid("1002");
//        // 提交事务
//        deleteSession.commit();
//
//        if(deleteResult > 0){
//            System.out.println("删除成功");
//        }else{
//            System.out.println("删除失败");
//        }
//        deleteSession.close();
    }
}